package com.css.fxfzysx.modules.vaomeFacilities.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.css.fxfzysx.base.response.RestResponse;
import com.css.fxfzysx.constants.FxfzConstants;
import com.css.fxfzysx.modules.vaomeFacilities.entity.CipvMatrixEntity;
import com.css.fxfzysx.modules.vaomeFacilities.repository.VmeFacilitiesRepository;
import com.css.fxfzysx.modules.vaomeFacilities.service.VmeFacilitiesService;
import com.css.fxfzysx.util.PlatformDateUtils;
import com.css.fxfzysx.util.PlatformPageUtils;
import com.css.fxfzysx.util.PlatformSessionUtils;
import com.css.fxfzysx.web.PlatformSessionContext;
import com.css.fxfzysx.zcpt.sys.service.SysRoleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class VmeFacilitiesServiceImpl implements VmeFacilitiesService {

    @Autowired
    private VmeFacilitiesRepository vmeFacilitiesRepository;

    @Autowired
    private SysRoleService roleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RestResponse updateData(List<CipvMatrixEntity> cipvMatrixList) {
        try {
            for (CipvMatrixEntity it : cipvMatrixList) {
                if (predict(it)) {
                    it.setUpdateUser(PlatformSessionContext.getUserID());
                    it.setUpdateTime(PlatformDateUtils.getCurrentTimestamp());
                    vmeFacilitiesRepository.updateData(it);
                } else {
                    return RestResponse.fail("失败,每行数据之和必须等于1");
                }
            }
            return RestResponse.succeed("成功!");
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    public static boolean predict(CipvMatrixEntity it) {
        BigDecimal result = it.getBasicIntact().add(it.getSlightDamage()).add(it.getModerateDamage()).add(it.getBadlyDamaged()).add(it.getCollapse());
        if (result.compareTo(BigDecimal.valueOf(1)) == 0) {
            return true;
        }
        return false;
    }

    @Override
    public RestResponse queryDataByNumber(String number) {
        try {
            return RestResponse.succeed(vmeFacilitiesRepository.getDataByNumber(number));
        } catch (Exception e) {
            String errorMessage = "失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }

    @Override
    public RestResponse queryCipVulnerability(CipvMatrixEntity cipvMatrixEntity, HttpServletRequest request) {
        try {
            int page = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int size = Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            String role = roleService.getRoleByUserId(PlatformSessionUtils.getUserId());
            List<Map> roleMaps = JSONObject.parseArray(role, Map.class);
            cipvMatrixEntity.setRole("1");
            for (Map roleMap : roleMaps) {
                String roleCode = (String) roleMap.get("roleCode");
                if (roleCode.equals(FxfzConstants.CZT_COUN_GL_ROLE) || roleCode.equals(FxfzConstants.CZT_PRI_GL_ROLE)) {
                    cipvMatrixEntity.setRole("1");
                }
            }
            return RestResponse.succeed(vmeFacilitiesRepository.queryCipVulnerability(cipvMatrixEntity, page, size));
        } catch (NumberFormatException e) {
            String message = "查看失败!";
            log.error(message, e);
            return RestResponse.fail(message);
        }
    }

    @Override
    public RestResponse deleteDataByNumber(String numbers) {
        try {
            for (String number : numbers.split(",")) {
                vmeFacilitiesRepository.deleteDataByNumber(number);
            }
            return RestResponse.succeed("删除成功!");
        } catch (Exception e) {
            String errorMessage = "删除失败!";
            log.error(errorMessage, e);
            return RestResponse.fail(errorMessage);
        }
    }
}
